package com.ls.blog.controller;
import static com.ls.blog.common.consts.Methods.*;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ls.blog.common.valid.Select;
import com.ls.blog.dto.Recodes;
import com.ls.blog.entity.Log;
import com.ls.blog.service.LogService;
import com.ls.blog.utils.IpUtil;
import com.ls.blog.utils.R;
import com.ls.blog.utils.Result;
import com.ls.blog.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author lishun
 * @since 2022-02-08
 */
@RestController
@RequestMapping("/log")
@Api(tags = "日志模块")
public class LogController {

    @Autowired
    private LogService logService;

    @ApiOperation(value = "日志列表",httpMethod = POST)
    @PostMapping("/list")
    public Result<Recodes<Log>> list(@Validated(Select.class) @RequestBody PageVO pageVO){
        Page<Log> page = new Page<>(pageVO.getCurrent(), pageVO.getPageSize());
        logService.page(page,new LambdaQueryWrapper<Log>().orderByAsc(Log::getDate));
        List<Log> records = page.getRecords();
        List<Log> logList = records.stream().peek(log -> log.setAddress(IpUtil.getIpSource(log.getIp()))).collect(Collectors.toList());
        Recodes<Log> logRecodes = new Recodes<>();
        logRecodes.setRecodes(logList);
        logRecodes.setTotal(page.getTotal());
        return R.ok(logRecodes);
    }

}
